﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Maticsoft.BLL;
using Maticsoft.Model;
using ExtendDLL;
using System.Text;
using System.Data;
using System.IO;

namespace lingxianWebservice
{
    /// <summary>
    /// getLine 的摘要说明
    /// </summary>
    public class getLine : IHttpHandler
    {
        private static readonly Maticsoft.BLL.BaseSql bll = new Maticsoft.BLL.BaseSql();
        public void ProcessRequest(HttpContext context)
        {

            context.Response.ContentType = "text/plain";
            try
            {


                string ZDID = context.Request["ZDID"].ToString().Trim();
                string ZDBH = context.Request["ZDBH"].ToString().Trim();
                string XSZT = context.Request["XSZT"].ToString().Trim();
                string callbackFunName = context.Request["callbackparam"];
                string returnString = string.Empty;
                string strTableName = string.Empty;
                string strSql = string.Empty;
                StringBuilder sb = new StringBuilder();
                sb.Append("SELECT TOP 1 ");
                if (XSZT == "1")
                {
                    strTableName = "ZB_Data_" + ZDID;
                    sb.Append(" CRSJ,SJ,[328] as lx_328,[329] as lx_329,[330] as lx_330,[331] as lx_331 FROM " + strTableName);


                }
                else if (XSZT == "2")
                {
                    strTableName = "ZB_Data_" + ZDID;
                    sb.Append("CRSJ,SJ,[332] as lx_332,[333] as lx_333 FROM " + strTableName);
                }
                else
                {
                    returnString = "error:错误状态";
                }
                sb.Append(" ORDER BY SJ DESC ");
                if (returnString.Length == 0)
                {
                    DataSet ds = bll.getList(sb.ToString());

                    if (null == ds)
                    {
                        returnString = "error:查无数据";
                    }
                    else
                    {
                        returnString = ds.Tables[0].toJSON();
                        returnString = callbackFunName + "(" + returnString + ")";

                        //context.Response.Write(str);
                    }
                }
                context.Response.Write(returnString);
            }
            catch (Exception ex)
            {

                StreamWriter log = new StreamWriter("ceshi.txt", true);

                log.WriteLine("time:" + System.DateTime.Now.ToLongTimeString() + ":::" + ex.ToString());

                log.Close();

                context.Response.Write(ex.ToString());

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}